Skip to main content

echo command

echo - display a line of text.

Usage: echo [OPTION]... [STRING]...

  • OPTION: Flags which enhances the echo abilities.
  • STRING: Text you want to display (optional; outputs a blank line if omitted).

Examples

  • Displaying text

    The most basic use of echo is to print text to the terminal.

    $ echo

    $ echo "Hello, world!"
    Hello, world!
    • Without arguments outputs a single newline (blank line).
  • Writing to a file

    Redirect echo output to a file using > (overwrite) or >> (append).

    $ echo "New content" > myfile.txt
    • Creates or overwrites myfile.txt with "New content".
    $ echo "More content" >> myfile.txt
    • Adds "More content" to the end of myfile.txt.
  • Using variables

    echo can display the value of shell variables

    $ NAME="Alice"
    $ echo "Hello, $NAME!"
    Hello, Alice!
    • Using curly braces would be ideal for better readability when variables are part of larger text.
    $ echo "Hello, Mr. ${NAME}!"
    Hello, Mr. Alice!
  • Handling newlines

    By default, echo adds a newline after the text. Use -n to suppress it.

    $ echo -n "No newline here" && echo "Next part"
    No newline hereNext part
    $ echo "No newline here" && echo "Next part"
    No newline here
    Next part
  • Interpreting escape sequences

    Use -e to enable interpretation of escape characters (like \n for newline or \t for tab).

    $ echo "Line 1\nLine 2"
    Line 1\nLine 2
    $ echo -e "Line 1\nLine 2\tTabbed"
    Line 1
    Line 2 Tabbed

    Common escape sequences:

    • \n: Newline
    • \t: Tab
    • \b: Backspace
    • \\: Literal backslash
  • Suppressing escape sequences

    Use -E (default in most shells) to disable escape interpretation explicitly.

    $ echo -E "Text with \n no newline"
    Text with \n no newline